﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace PhiStore.ExampleCodes.PhiElection
{
    public partial class ElectionReader : Form
    {
        Timer t = new Timer();

        ElectionResultReader err;
        public ElectionReader()
        {
            InitializeComponent();
            t.Tick += new EventHandler(UpdateUI);
            t.Interval = 1000;
            t.Start();

            ServerList.ColumnCount = 2;
            ServerList.Columns[0].Name = "IP";
            ServerList.Columns[1].Name = "Port";
            ServerList.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            ServerList.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            ServerList.MultiSelect = false;
            ServerList.ReadOnly = true;
            ServerList.RowHeadersVisible = false;
            ServerList.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            ServerList.ShowCellErrors = false;
            ServerList.ShowCellToolTips = false;
            ServerList.ShowEditingIcon = false;
            ServerList.ShowRowErrors = false;

            ElectionPercentages.ColumnCount = 2;
            ElectionPercentages.Columns[0].Name = "Party Name";
            ElectionPercentages.Columns[1].Name = "% of votes";
            ElectionPercentages.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            ElectionPercentages.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            ElectionPercentages.MultiSelect = false;
            ElectionPercentages.ReadOnly = true;
            ElectionPercentages.RowHeadersVisible = false;
            ElectionPercentages.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            ElectionPercentages.ShowCellErrors = false;
            ElectionPercentages.ShowCellToolTips = false;
            ElectionPercentages.ShowEditingIcon = false;
            ElectionPercentages.ShowRowErrors = false;
        }

        void UpdateUI(object sender, EventArgs e)
        {
            if (err != null)
            {
                ElectionResult er = err.Result;
                Throughput.Text = err.ThroughPut.ToString("N2") + " reads/s";

                double percentagecounted = 
                    Convert.ToDouble( er.VotesCounted) / Convert.ToDouble(er.VotesCounted + er.VotesRemaining);

                PercentageCounted.Text = (percentagecounted * 100.0).ToString("N2") + " %";

                ElectionPercentages.Rows.Clear();
                foreach(string party in er.PartyPercentage.Keys)
                {
                    string[] row = new string[] {party, er.PartyPercentage[party].ToString("N1")};
                    ElectionPercentages.Rows.Add(row);

                }

                if(er.VotesRemaining == 0) //done
                    StartStop_Click(sender, e);
            }
        }

        private void StartStop_Click(object sender, EventArgs e)
        {
            if (StartStop.Text == "Start")
            {
                if(ServerList.SelectedCells.Count == 0){
                    MessageBox.Show("Select a server from the list");
                    return;
                }

                BroadcastObject bc = bcs[ServerList.SelectedCells[0].RowIndex];

                StartStop.Text = "Stop";
                err = new ElectionResultReader(bc);
            }
            else
            {

                if (err != null)
                    err.STOP();

                err = null;
                StartStop.Text = "Start";
            }
        }

        private List<BroadcastObject> bcs = new List<BroadcastObject>();
        private void GenerateServerList_Click(object sender, EventArgs e)
        {

            bcs = new List<BroadcastObject>();
            for (int i = 0; i < 5; i++)
            {
                BroadcastObject bc = ReceiveBroadcast.Receive(24281, 1);
                if (!bcs.Contains(bc))
                {
                    bcs.Add(bc);
                }
            }
            UpdateServerList();

        }

        private void UpdateServerList()
        {
            ServerList.Rows.Clear();
            string[] row;
            foreach (BroadcastObject bc in bcs)
            {
                row = new string[] {
                    bc.IP.ToString(), bc.Port.ToString()
                };
                ServerList.Rows.Add(row);
            }
        }
    }
}
